package com.info3k.client.commands.event;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.datepicker.client.DateBox;
import com.info3k.client.Info3K;
import com.info3k.client.commands.location.SelectLocationCommand;
import com.info3k.client.components.ContentBox;
import com.info3k.client.components.InfoWindow;
import com.info3k.client.components.validators.RichTextAreaValidator;
import com.info3k.client.components.validators.TextBoxValidator;
import com.info3k.client.domain.Event;
import com.info3k.client.domain.Location;
import com.info3k.client.enums.TabTypes;


public class EditEventCommand extends ContentBox implements Command {

	private RichTextAreaValidator description = new RichTextAreaValidator();
	private TextBoxValidator title = new TextBoxValidator();
	private DateBox datePicker = new DateBox();
	
	private Button editButton = new Button(Info3K.msgs.editEvent());
	
	private Button addLocationsButton = new Button(Info3K.msgs.chooseEventLocations() + "[0]");
	
	private SelectLocationCommand selectLocationCommand;
	
	private Info3K info3k;
	
	private Location[] locations;
	
	private EditEventCommand parentChild;
	
	private Event event;
	
	public EditEventCommand(Info3K info, Event ev){
		this.info3k = info;
		this.event = ev;
		parentChild = this;
		content.setWidth("610px");
		
		title.setWidth("98%");
		description.setWidth("98%");
		description.setHeight("80px");
		
		description.setText(event.getDescription());
		title.setText(event.getTitle());
		datePicker.setValue(event.getEventTime());
		setLocations(ev.getLocations());
		title.setValid(true);
		
		datePicker.setWidth("180px");
		
		datePicker.setFormat(new DateBox.DefaultFormat());
		
		editButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent e) {
				if(title!=null){
					event.setDescription(description.getText());
					event.setTitle(title.getText());
					event.setLocations(locations);
					event.setEventTime(datePicker.getValue());
					
					Info3K.loader.setVisible(true);
					Info3K.infoService.addEvent(event, new AsyncCallback<Void>(){
						public void onFailure(Throwable caught) {
							Info3K.loader.setVisible(false);
							InfoWindow.showMessage(Info3K.msgs.error(), caught.getMessage());
						}
	
						public void onSuccess(Void result) {
							Info3K.loader.setVisible(false);
							InfoWindow.showMessage(Info3K.msgs.ok(), Info3K.msgs.eventEditedSuccessfully());
							info3k.getTabManager().refresh(TabTypes.LATEST_EVENTS_TOP);
							info3k.getTabManager().refresh(TabTypes.LATEST_EVENTS_LEFT);
							hide();
							ShowEventCommand cmd = new ShowEventCommand(RootPanel.get("right_section"), info3k, event);
							cmd.execute();
						}
					});
				} else InfoWindow.showMessage(Info3K.msgs.error(), Info3K.msgs.pleaseFillAllFields());
			}
		});
		
		setText(Info3K.msgs.editEvent());		
		
		final Label lblName = new Label(Info3K.msgs.title());
		final HTML lblDescription = new HTML("<br>" + Info3K.msgs.eventDescription());
		final Label lblEventDate = new Label(Info3K.msgs.eventDate());
		
		content.add(lblName);
		content.add(title);
		content.add(lblEventDate);
		content.add(datePicker);
		content.add(lblDescription);
		content.add(description);
		
		addLocationsButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				selectLocationCommand = new SelectLocationCommand(parentChild, info3k);
				selectLocationCommand.execute();
			}
		});
		
		title.addChangeHandler(new ChangeHandler(){
			public void onChange(ChangeEvent event) {
				title.setValid(title.getText().length()>0);
			}
		});
		
		footer.add(addLocationsButton);
		addLocationsButton.getElement().getStyle().setProperty("margin", "0 10px 0 0");
		footer.add(editButton);
	}

	public void execute() {
		center();
	}
	
	private void renderAddresses(){
		addLocationsButton.setText(Info3K.msgs.chooseEventLocations() + " [" + locations.length + "]");
	}

	public void setLocations(Location[] locations) {
		this.locations = locations;
		renderAddresses();
	}

	public Location[] getLocations() {
		return locations;
	}
}